/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.pay;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.common.SignUtils;

public class OrderInfoUtil
{
	/**
	 * 构造支付订单参数列表
	 * 
	 * @param pid
	 * @param app_id
	 * @param target_id
	 * @return
	 */
	public static Map<String, String> buildOrderParamMap(String app_id, boolean rsa2, String total_amount, String subject, String body, String out_trade_no)
	{
		String strnotifyUrl = "";
		try
		{
			strnotifyUrl = URLEncoder.encode("http://xxx.xx.xx.xxx:8888/AliPay.html",Com_Para.U_CODE);
		}
		catch(Exception e)
		{
		}
		Map<String, String> keyValues = new HashMap<String, String>();
		keyValues.put("app_id",app_id);
		keyValues.put("biz_content","{\"timeout_express\":\"30m\",\"product_code\":\"QUICK_MSECURITY_PAY\",\"total_amount\":\""
				+ total_amount + "\",\"subject\":\"" + subject
				+ "\",\"body\":\"" + body + "\",\"out_trade_no\":\""
				+ out_trade_no + "\"}");
		keyValues.put("charset","utf-8");
		keyValues.put("method","alipay.trade.app.pay");
		keyValues.put("sign_type",rsa2 ? "RSA2" : "RSA");
		keyValues.put("timestamp",Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss"));
		keyValues.put("notify_url",strnotifyUrl);
		keyValues.put("version","1.0");
		return keyValues;
	}

	public static Map<String, String> buildAuthSendParamMap(String app_id, boolean rsa2, String pid, String target_id)
	{
		Map<String, String> keyValues = new HashMap<String, String>();
		// 服务对应的名称，常量值为com.alipay.account.auth
		keyValues.put("apiname","com.alipay.account.auth");
		// 接口名称，常量值为alipay.open.auth.sdk.code.get
		keyValues.put("method","alipay.open.auth.sdk.code.get");
		// app_id
		keyValues.put("app_id","xxxxxx");// app_id
		// app_name
		keyValues.put("app_name","mc");
		// 业务类型， 固定值
		keyValues.put("biz_type","openservice");
		keyValues.put("pid","xxxxx");// pid
		// 产品码， 固定值
		keyValues.put("product_id","APP_FAST_LOGIN");
		// 授权范围， 固定值
		keyValues.put("scope","kuaijie");
		// 商户唯一标识，如：kkkkk091125
		keyValues.put("target_id",target_id);
		// 授权类型， 固定值
		keyValues.put("auth_type","AUTHACCOUNT");
		// 签名类型
		keyValues.put("sign_type",rsa2 ? "RSA2" : "RSA");

		return keyValues;
	}

	/**
	 * 构造支付订单参数信息
	 * 
	 * @param map
	 *            支付订单参数
	 * @return
	 */
	public static String buildOrderParam(Map<String, String> map)
	{
		List<String> keys = new ArrayList<String>(map.keySet());

		StringBuilder sb = new StringBuilder();
		for(int i = 0;i < keys.size() - 1;i++)
		{
			String key = keys.get(i);
			String value = map.get(key);
			sb.append(buildKeyValue(key,value,true));
			sb.append("&");
		}

		String tailKey = keys.get(keys.size() - 1);
		String tailValue = map.get(tailKey);
		sb.append(buildKeyValue(tailKey,tailValue,true));

		return sb.toString();
	}

	/**
	 * 拼接键值对
	 * 
	 * @param key
	 * @param value
	 * @param isEncode
	 * @return
	 */
	private static String buildKeyValue(String key, String value, boolean isEncode)
	{
		StringBuilder sb = new StringBuilder();
		sb.append(key);
		sb.append("=");
		if(isEncode)
		{
			try
			{
				sb.append(URLEncoder.encode(value,"UTF-8"));
			}
			catch(UnsupportedEncodingException e)
			{
				sb.append(value);
			}
		}else
		{
			sb.append(value);
		}
		return sb.toString();
	}

	/**
	 * 对支付参数信息进行签名
	 * 
	 * @param map
	 *            待签名授权信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String getSign(Map<String, String> map, String rsaKey, boolean rsa2)
	{
		List<String> keys = new ArrayList<String>(map.keySet());
		// key排序
		Collections.sort(keys);

		StringBuilder authInfo = new StringBuilder();
		for(int i = 0;i < keys.size() - 1;i++)
		{
			String key = keys.get(i);
			String value = map.get(key);
			authInfo.append(buildKeyValue(key,value,false));
			authInfo.append("&");
		}

		String tailKey = keys.get(keys.size() - 1);
		String tailValue = map.get(tailKey);
		authInfo.append(buildKeyValue(tailKey,tailValue,false));

		String oriSign = "";
		String encodedSign = "";

		try
		{	oriSign = SignUtils.sign(authInfo.toString(),rsaKey,rsa2);
			encodedSign = URLEncoder.encode(oriSign,"UTF-8");
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,OrderInfoUtil.class.toString());
		}
		return "sign=" + encodedSign;
	}
}